package com.shop.controller;

import com.shop.bean.Category;
import com.shop.bean.Order_details;
import com.shop.bean.Product;
import com.shop.bean.Product_images;
import com.shop.service.ICategoryService;
import com.shop.service.IProductService;
import com.shop.util.LayData;
import com.shop.util.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    IProductService productService;

    @Autowired
    ICategoryService CategoryService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text) {
        List<Product> list=productService.selectProducts(page, text);
        int count=productService.getCount(text);
        return new LayData(count, list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int delete(int id){
        return productService.deleteProduct(id);
    }

    @RequestMapping("putOn")
    @ResponseBody
    public int putOn(int id){

        return productService.putOn(id);
    }

    @RequestMapping("putOff")
    @ResponseBody
    public int putOff(int id){
        System.out.println("下架"+id);
        return productService.putOff(id);
    }


    @RequestMapping("getChildren")
    @ResponseBody
    public List<Product> getChildren(int id){
        return productService.getChildren(id);
    }


    @RequestMapping("add")
    @ResponseBody
    public void add(@RequestBody Product product){
        // 假设这里处理一级分类和二级分类
        int categoryId1 = product.getCategory_id1();
        int categoryId2 = product.getCategory_id2();
        // 可以根据 categoryId1 和 categoryId2 去查询对应的 Category 对象
        // 这里简单模拟，假设已经有查询方法
        Category category1 = CategoryService.getCategoryById(categoryId1);
        Category category2 = CategoryService.getCategoryById(categoryId2);
        product.setCategory1(category1);
        product.setCategory2(category2);
        productService.addProductWithImages(product);
    }


    @RequestMapping("edit")
    @ResponseBody
    public void edit(@RequestBody Product product){
        // 假设这里处理一级分类和二级分类
        int categoryId1 = product.getCategory_id1();
        int categoryId2 = product.getCategory_id2();
        // 可以根据 categoryId1 和 categoryId2 去查询对应的 Category 对象
        // 这里简单模拟，假设已经有查询方法
        Category category1 = CategoryService.getCategoryById(categoryId1);
        Category category2 = CategoryService.getCategoryById(categoryId2);
        product.setCategory1(category1);
        product.setCategory2(category2);
        productService.editProductWithImages(product);
    }

    //要最新的六个商品
    @RequestMapping("getNew8")
    @ResponseBody
    public LayData getNew8() {
        List<Product> list=productService.getNew8();
        return new LayData(6, list);
    }


    @RequestMapping("getproductById")
    @ResponseBody
    public LayData getproductById(Integer id){
        Product product=productService.getProductById(id);
        return LayData.success(product);
    }

    @RequestMapping("getProductByCategoryAndName")
    @ResponseBody
    public LayData getProductByCategoryAndName(Product product, Page page, String text){

        List<Product> list=productService.getProductByCategoryAndName(product,page,text);
        int count=productService.getCounts(text,product);
        return new LayData(count , list);
    }

    /*    @RequestMapping("list")
    public String list(Product categoryid, String keywords, Model model){
        model.addAttribute("categoryid",categoryid);
        model.addAttribute("keywords",keywords);
        return "shopping/list.html";
    }*/

    @RequestMapping("decStock")
    @ResponseBody
    public LayData decStock(Integer productid,Integer quantity){

        productService.decStock(productid,quantity);
        return LayData.success("生成成功了");
    }


}
